var self = this
importScripts('libffmpeg_.js')

Module.onRuntimeInitialized = function() { //此时才能获得完整Module对象
	console.log(Module);
	self.test.onLoadFlag = true;
	self.test.initWorker();
}
function Test2() {
}
Test2.prototype.decoder = function (data) {//u8Array
	var cacheBuffer = Module._malloc(data.length);
	Module.HEAPU8.set(data, cacheBuffer);
	var ret = Module._decodeData(cacheBuffer, data.length, 0);
	Module._flushDecoder();
}

Test2.prototype.initWorker = function () {
    console.log('初始化worker ！')
	let videoCallback = Module.addFunction(function (addr_y, addr_u, addr_v, stride_y, stride_u, stride_v, width, height, pts) {
    console.log("[%d]In video callback, size = %d * %d, pts = %d", ++videoSize, width, height, pts)
    let out_y = Module.HEAPU8.subarray(addr_y, addr_y + stride_y * height)
    let out_u = Module.HEAPU8.subarray(addr_u, addr_u + (stride_u * height) / 2)
    let out_v = Module.HEAPU8.subarray(addr_v, addr_v + (stride_v * height) / 2)
    let buf_y = new Uint8Array(out_y)
    let buf_u = new Uint8Array(out_u)
    let buf_v = new Uint8Array(out_v)
    let data = new Uint8Array(buf_y.length + buf_u.length + buf_v.length)
    data.set(buf_y, 0)
    data.set(buf_u, buf_y.length)
    data.set(buf_v, buf_y.length + buf_u.length)
    var obj = {
        data: data.buffer,
        width,
        height
    }
	self.postMessage(obj,[obj]);
},"vi");
    var ret = Module._openDecoder(0, videoCallback, 2);
    if(ret == 0) {
        console.log("openDecoder success");
	}
}

self.test = new Test2();

self.onmessage = function (evt) {
    console.log(`子线程Reveived msg: ${evt.data}`)
	if(self.test.onLoadFlag){
		self.test.decoder(evt.data);
	}
      
}


